package com.zzyl.nursing.service.impl;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zzyl.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ContractMapper;
import com.zzyl.nursing.domain.Contract;
import com.zzyl.nursing.service.IContractService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.CollectionUtils;

/**
 * 合同Service业务层处理
 * 
 * @author alexis
 * @date 2025-09-04
 */
@Service
public class ContractServiceImpl extends ServiceImpl<ContractMapper, Contract> implements IContractService
{
    @Autowired
    private ContractMapper contractMapper;

    /**
     * 查询合同
     * 
     * @param id 合同主键
     * @return 合同
     */
    @Override
    public Contract selectContractById(Long id)
    {
        return contractMapper.selectById(id);
    }

    /**
     * 查询合同列表
     * 
     * @param contract 合同
     * @return 合同
     */
    @Override
    public List<Contract> selectContractList(Contract contract)
    {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增合同
     * 
     * @param contract 合同
     * @return 结果
     */
    @Override
    public int insertContract(Contract contract)
    {
        return contractMapper.insert(contract);
    }

    /**
     * 修改合同
     * 
     * @param contract 合同
     * @return 结果
     */
    @Override
    public int updateContract(Contract contract)
    {
        return contractMapper.updateById(contract);
    }

    /**
     * 批量删除合同
     * 
     * @param ids 需要删除的合同主键
     * @return 结果
     */
    @Override
    public int deleteContractByIds(Long[] ids)
    {
        return contractMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除合同信息
     * 
     * @param id 合同主键
     * @return 结果
     */
    @Override
    public int deleteContractById(Long id)
    {
        return contractMapper.deleteById(id);
    }

    /**
     * 更新合同状态-已生效
     */
    @Override
    public void updateContractStatus() {
        //1.查询所有需要更新状态的合同 where start_date <= now() and status = 0
        LambdaQueryWrapper<Contract> qw = new LambdaQueryWrapper<>();
        qw.lt(Contract::getStartDate, DateUtils.getNowDate())
                .eq(Contract::getStatus,0);
        List<Contract> contracts = contractMapper.selectList(qw);
        if (CollectionUtils.isEmpty( contracts)){
            return;
        }
        //2.更新合同状态(其状态为1)
        LambdaUpdateWrapper<Contract> uqw = new LambdaUpdateWrapper<>();
        List<Long> idList = contracts.stream().map(Contract::getId).collect(Collectors.toList());
        uqw.in(Contract::getId,idList)
                .set(Contract::getUpdateTime, LocalDateTime.now())
                .set(Contract::getStatus,1);
        contractMapper.update(null,uqw);
    }
}
